local RSNG = RSNG
local AceOO = AceLibrary("AceOO-2.0")
local AceEvent = AceLibrary("AceEvent-2.0")
local L = AceLibrary("Ace-Locale-2.2"):new("RSNG")
local C = RSNG.Classes

local Filter = AceOO.Class(C.IFilter, C.ISerializable)

local IgnoreList = {}

local UpdateIgnoreList = function ()
	for k in pairs(IgnoreList) do
		IgnoreList[k] = nil
	end
	for i = 1, GetNumIgnores() do
		IgnoreList[GetIgnoreName(i)] = true
	end
end

local updater
local activate = function ()
	if not updater then
		updater = {}
		AceEvent:embed(updater)
		updater:RegisterBucketEvent("IGNORELIST_UPDATE", 0.5, UpdateIgnoreList)
		UpdateIgnoreList()
	end
end

function Filter.prototype:init()
	Filter.super.prototype.init(self)
end

function Filter.prototype:Filter(unit, name, class, group, dead, online)
	activate()
	local ignore = IgnoreList[name]
	if self.include then
		return ignore
	else
		return not ignore
	end
end

function Filter.prototype:Serialize(type, data)
	if type == "save" then
		data.include = self.include
	elseif type == "load" then
		self.include = data.include
	end
end

function Filter.prototype:GetOptionTable()
	local t = self.options
	if not t then
		t = {
			type = "toggle",
			name = L["Ignore"],
			desc = L["Filter units that are in your ignore list."],
			get = function () return self.include end,
			set = function (value) self.include = value and true or false end,
		}
		self.options = t
	end
	return t
end

RSNG:RegisterFilter(L["Ignore Filter"], Filter)
